/*
*  Copyright 2019-2023 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.poemmanager.rest;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.AnonymousAccess;
import me.zhengjie.annotation.Log;
import me.zhengjie.poemPoet.domain.PoemPoetLike;
import me.zhengjie.poemPoet.service.PoemPoetLikeService;
import me.zhengjie.poemUser.domain.PoemUserLike;
import me.zhengjie.poemUser.service.PoemUserLikeService;
import me.zhengjie.poemmanager.domain.ObjectLike;
import me.zhengjie.poemmanager.domain.vo.ObjectLikeQueryCriteria;
import me.zhengjie.poemmanager.service.CommentLikeService;
import me.zhengjie.poemmanager.service.ObjectLikeService;
import me.zhengjie.post.domain.PostLike;
import me.zhengjie.post.service.PostLikeService;
import me.zhengjie.recitation.domain.RecitationLike;
import me.zhengjie.recitation.service.RecitationLikeService;
import me.zhengjie.utils.PageResult;
import org.springframework.beans.BeanUtils;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
* @author hwl
* @date 2024-12-11
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "like管理")
@RequestMapping("/api/objectLike")
public class ObjectLikeController {

    private final ObjectLikeService objectLikeService;
    private final PoemPoetLikeService poemPoetLikeService;
    private final PoemUserLikeService poemUserLikeService;
    private final PostLikeService postLikeService;
    private final RecitationLikeService recitationLikeService;
    private final CommentLikeService commentLikeService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @AnonymousAccess
    public void exportObjectLike(HttpServletResponse response, ObjectLikeQueryCriteria criteria) throws IOException {
        objectLikeService.download(objectLikeService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询like")
    @ApiOperation("查询like")
    @AnonymousAccess
    public ResponseEntity<PageResult<ObjectLike>> queryObjectLike(ObjectLikeQueryCriteria criteria, Page<Object> page){
        return new ResponseEntity<>(objectLikeService.queryAll(criteria,page),HttpStatus.OK);
    }

    @GetMapping(value = "/paged")
    @Log("分页查询like")
    @ApiOperation("分页查询like")
    @AnonymousAccess
    public ResponseEntity<PageResult<ObjectLike>> pagedQueryObjectLike(ObjectLikeQueryCriteria criteria,
                                                                       @RequestParam(defaultValue = "1") int current,
                                                                       @RequestParam(defaultValue = "20") int size  ){
        Page<Object> page = new Page<>(current,size);
        return new ResponseEntity<>(objectLikeService.queryAll(criteria,page),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增like")
    @ApiOperation("新增like")
    @AnonymousAccess
    public ResponseEntity<Object> createObjectLike(@Validated @RequestBody ObjectLike resources){
        ObjectLike objectLike = null ;
        switch (resources.getObjectType()) {
            case 0:
                objectLike = postLikeService.create(convertToPostLike(resources));
                break;
            case 1:
                objectLike = poemUserLikeService.create(convertToPoemUserLike(resources));
                break;
            case 2:
                objectLike = poemPoetLikeService.create(convertToPoemPoetLike(resources));
                break;
            case 3:
                objectLike = recitationLikeService.create(convertToRecitationLike(resources));
                break;
            case 4:
                objectLike = commentLikeService.create(resources);
                break;
            case 5:
                objectLike = objectLikeService.create(resources);
                break;
            default:
                throw new IllegalArgumentException("无效的评论类型: " + resources.getObjectType());
        }
        return ResponseEntity.ok(objectLike);
    }

    @DeleteMapping
    @Log("删除like")
    @ApiOperation("删除like")
    @AnonymousAccess
    public ResponseEntity<Object> deleteObjectLike(@RequestParam Integer type ,
                                                   @RequestParam (defaultValue = "0") Integer commentType,
                                                   @RequestBody  List<Integer> ids) {
        switch (type) {
            case 0:
                postLikeService.deleteAll(ids);
                break;
            case 1:
                poemUserLikeService.deleteAll(ids);
                break;
            case 2:
                poemPoetLikeService.deleteAll(ids);
                break;
            case 3:
                recitationLikeService.deleteAll(ids);
                break;
            case 4:
                commentLikeService.deleteAll(commentType,ids);
                break;
            case 5:
                objectLikeService.deleteAll(ids);
                break;
            default:
                throw new IllegalArgumentException("无效的点赞类型: " + type);
        }
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @GetMapping("isLike")
    @Log("是否点赞")
    @ApiOperation("是否点赞")
    @AnonymousAccess
    public ObjectLike isLike(@RequestParam Integer objectId,@RequestParam Integer objectType,
                             @RequestParam (defaultValue = "0") Integer commentType,
                             @RequestParam String userName) {
        ObjectLike like=new ObjectLike();
        like.setObjectId(objectId);
        like.setObjectType(objectType);
        like.setUserName(userName);
        switch (like.getObjectType()) {
            case 0:
                return postLikeService.isLike(convertToPostLike(like));
            case 1:
                return poemUserLikeService.isLike(convertToPoemUserLike(like));
            case 2:
                return poemPoetLikeService.isLike(convertToPoemPoetLike(like));
            case 3:
                return recitationLikeService.isLike(convertToRecitationLike(like));
            case 4:
                return commentLikeService.isLike(commentType,like);
            case 5:
                return  objectLikeService.isLike(like);
            default:
                throw new IllegalArgumentException("无效的点赞类型: " + objectType);
        }

    }

    // 转换方法组（返回具体子类）
    private PostLike convertToPostLike(ObjectLike origin) {
        PostLike target = new PostLike();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private PoemUserLike convertToPoemUserLike(ObjectLike origin) {
        PoemUserLike target = new PoemUserLike();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private PoemPoetLike convertToPoemPoetLike(ObjectLike origin) {
        PoemPoetLike target = new PoemPoetLike();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

    private RecitationLike convertToRecitationLike(ObjectLike origin) {
        RecitationLike target = new RecitationLike();
        BeanUtils.copyProperties(origin, target, "objectType");
        return target;
    }

}